package cn.dylan.pet.controller;

import cn.dylan.basic.annotation.PreAuthorize;
import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.LogininfoContext;
import cn.dylan.basic.util.PageList;

import cn.dylan.pet.domain.Pet;
import cn.dylan.pet.domain.PetDetail;
import cn.dylan.pet.mapper.PetMapper;
import cn.dylan.pet.query.PetQuery;
import cn.dylan.pet.service.IPetService;
import cn.dylan.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    
    @Autowired
    private IPetService petService;


    /**
     *
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    @PreAuthorize(name = "查询所有宠物",sn = "pet:get")
    public List<Pet> loadAll(HttpServletRequest request){
        Logininfo logininfo = LogininfoContext.getLogininfo(request);
        return petService.loadAll();
    }

    /**
     * GetMapping 请求
     * @requstMapping(value = "", method = "get")
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Pet loadById(@PathVariable("id") Long id){
        System.out.println(id);
        return petService.LoadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个部门" )
    public JsonResult delete(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            petService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 修改和添加
     * @param pet
     * @return
     */
    @PostMapping
    @ApiOperation(value = "修改或者添加" )
    public JsonResult updateOrAdd(@RequestBody Pet pet){
        try {
            if(pet.getId()==null){
                //添加操作
                petService.save(pet);
            }else {
                petService.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PutMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Pet> QueryPage(@RequestBody PetQuery query){
        try {
            return petService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            petService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


    @PostMapping("/onsale")
    public JsonResult onSale(@RequestBody List<Long> ids){
        return petService.onSale(ids);
    }

    @PostMapping("/offsale")
    public JsonResult offSale(@RequestBody List<Long> ids){
        return petService.offSale(ids);
    }





    @GetMapping("/detail/{id}")
    public PetDetail detailQuery(@PathVariable("id") Long id){
        System.out.println("***************************"+id);
        PetDetail petDetail = petService.LoadPetDetailById(id);
        return petDetail;
    }
    
}
